/*
 * downloader.cpp
 *
 *  Created on: May 20, 2010
 *      Author: deroy2
 */

#include <QApplication>
#include <QStringList>
#include <QFile>
#include <QList>
#include <QDebug>
#include <QString>
#include <QMessageBox>
#include <QByteArray>
#include <qxml.h>
#include <qsettings>
#include "QCalAuthRetriever.h"

QCalAuthRetriever::QCalAuthRetriever(QWidget *parent)
	    : QObject(parent)
    
{ 
	
	manager = new QNetworkAccessManager(this);
	connect(manager, SIGNAL(finished(QNetworkReply*)),this,SLOT(downloadCompleted(QNetworkReply*)));
	

}


QCalAuthRetriever::~QCalAuthRetriever()
{
	
	delete manager, manager=0;
	
}

void QCalAuthRetriever::close()
{
    

    disconnect(manager, 0, this, 0);
    QCoreApplication::removePostedEvents(this, QEvent::MetaCall);

    delete manager, manager=0;
 
}

void QCalAuthRetriever::load(const QString& url,const QString& name,const QString& pass)
{
	//dncmplt=false;
	QNetworkRequest request(url);
	request.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded");
	QByteArray data;
    data.append(QString(QString("accountType=GOOGLE") + QString("&Email=")+ name + QString("&Passwd=")+ pass + QString("&service=cl") + QString("&source=mycompany-GBlogger-1.0")).toUtf8());
    QNetworkReply* reply = manager->post(request,data);
    qDebug("QNetworkReply %X\n",reply);
    connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(error(QNetworkReply::NetworkError)));	
	Lname =name;
	LPassWord =pass;       
	
}

void QCalAuthRetriever::downloadCompleted(QNetworkReply* reply)
{
	
    qDebug("QNetworkReply %X\n",reply);
    
/*    const QUrl url = reply->url();
    QString name = url.host() + url.path();
    name.replace("/","_");
    name.replace("\\","_");*/
 
    //The content of the downloaded file is saved in this path    
    QFile file("c:\\data\\afile.txt");
    file.open(QIODevice::ReadWrite | QIODevice::WriteOnly);
    char buffer[1024];
        while(true) {
            qint64 len = reply->read(buffer,1024);
            if(len < 1) {
                break;
            }
            file.write(buffer,len);
        }      
   
  
    file.close();
    reply->close();
    
    
    QMessageBox msgBox;
    msgBox.setText("download done");
    msgBox.exec(); 
    
    qDebug("Download done");
    if(reply->error()==QNetworkReply::NoError){
    QSettings setting("Nokia", "QTnetworkGUI");
	//setting.clear();
   	setting.setValue("Name",Lname);
	setting.setValue("Pass",LPassWord);
	
	emit downloadCompleted();
	
//	QString url="https://www.google.com/calendar/feeds/debjitroy.dev@gmail.com/public/full";
//	upldr = new uploader;
//	upldr->load(url);

    }
}

void QCalAuthRetriever::error(QNetworkReply::NetworkError aErr)
{
	
    QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
    QString strerr;
    strerr=QString("Network Error:%1")
    		.arg(aErr);
    QMessageBox msgBoxErr;
     msgBoxErr.setText(strerr);
     msgBoxErr.exec();
    qDebug("Network error:%d \n",aErr);
    qCritical("%s",reply->errorString().toStdString().c_str());
    
    
}

